<?php
  include("include/conn.php");
  
  if(isset($_REQUEST['bhatthipartyId']))
    $totalId = $_REQUEST['bhatthipartyId'];
  else
	  $totalId = $_REQUEST['total_ids'];
	  
	$totalId=split(",",$totalId);
	$deleteId="";
	for($i=0;$i < count($totalId);$i++) //to skip extra "0" that we have taken when passing form argument
	{

    if(isset($_REQUEST['bhatthipartyId']))
 		  @$checkboxvalue= $_REQUEST['bhatthipartyId'][$i];
    else
		  @$checkboxvalue=$_REQUEST['C1_'.$totalId[$i]];

		if(isset($checkboxvalue))
		{
			$deleteId.=",".$totalId[$i];
		}
	}
	if($deleteId!="")
	{
		$deleteId=substr($deleteId,1);
    
    $selectBhatthiBook = "SELECT bhatthiBookId FROM bhatthibook
                           WHERE bhatthiPartyId IN (".$deleteId.")";
    $selectBhatthiBookRes = mysql_query($selectBhatthiBook);
    $bhatthiId = "0,";
    while($bhatthiRow = mysql_fetch_assoc($selectBhatthiBookRes))
    {
      $bhatthiId .= $bhatthiRow['bhatthiBookId'].",";
    }
    $bhatthiBookDetail = "SELECT bhatthibook.bhatthiDate, bhatthibookdetail.particulars, bhatthibookdetail.fine, bhatthibookdetail.amount,
                                 bhatthibookdetail.CrDr, bhatthiparty.bhatthipartyName
		                        FROM bhatthibook
		                        JOIN bhatthibookdetail ON bhatthibookdetail.bhatthiBookId = bhatthibook.bhatthiBookId
		                        JOIN bhatthiparty ON bhatthiparty.bhatthipartyId = bhatthibook.bhatthiPartyId
		                       WHERE bhatthibook.bhatthiPartyId IN (".$deleteId.")
		                         AND bhatthibookdetail.inRojmel = 1
		                       ORDER BY bhatthibook.bhatthiDate";
		$bhatthiBookDetailRes = mysql_query($bhatthiBookDetail);
    while($bhatthiRow = mysql_fetch_assoc($bhatthiBookDetailRes))
    {
    	$insertRojmel = "INSERT INTO rojmel (rojmelDate, particulars, fine, amount, CrDr)
    	                 VALUES ('".$bhatthiRow['bhatthiDate']."','".$bhatthiRow['particulars']."(".$bhatthiRow['bhatthipartyName'].")',
    	                         ".$bhatthiRow['fine'].",".$bhatthiRow['amount'].",'".$bhatthiRow['CrDr']."')";
      $insertRojmelRes = mysql_query($insertRojmel);
    }
    $bhatthiBook = "DELETE FROM bhatthibook
                     WHERE bhatthiPartyId IN (".$deleteId.")";
    $bhatthiBookRes = mysql_query($bhatthiBook);
    
    $bhatthiBookDetail = "DELETE FROM bhatthibookdetail
                           WHERE bhatthiBookId IN (".substr($bhatthiId,0,-1).")";
    $bhatthiBookDetailRes = mysql_query($bhatthiBookDetail);
    
	  $query1 = "DELETE FROM bhatthiparty 
	              WHERE bhatthipartyId IN (".$deleteId.")";
	  $result1=mysql_query($query1);
	}
	
	mysql_close($con) or die("error closing connection");
  header("Location:bhatthiPartyList.php");
	exit();
 ?>